<?php

use console\db\Migration;

/**
 * 系统应用表单相关表
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m150104_091360_t_form extends Migration {

    /**
     * {@inheritdoc}
     */
    public function safeUp() {

        /**
         * 应用表单表
         * @table `form`
         */
        $this->tableBuilder([
            'name' => 'form',
            'comment' => '应用表单表',
            'columns' => [
                'id' => $this->primaryKey(),
                'name' => $this->char(20)->notNull()->unique()->comment('表单标识名称'),
                'label' => $this->string(50)->notNull()->comment('名称'),
                'description' => $this->string(255)->defaultValue('')->comment('说明'),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'type' => $this->tinyint(1)->unsigned()->notNull()->defaultValue(0)->comment('表单类型'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(10)->comment('状态'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
                'deleted_at' => $this->unixTimestamp()->comment('删除时间'),
            ]
        ]);

        /**
         * 应用表单字段分组表
         * @table `form_field_group`
         */
        $this->tableBuilder([
            'name' => 'form_field_group',
            'comment' => '应用表单字段分组表',
            'columns' => [
                'id' => $this->primaryKey(),
                'form_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('表单ID'),
                'name' => $this->string(50)->notNull()->comment('组名称'),
                'description' => $this->string(255)->defaultValue('')->comment('描述'),
                'listorder' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('显示排序'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(10)->comment('状态'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
                'deleted_at' => $this->unixTimestamp()->comment('删除时间'),
            ]
        ]);

        /**
         * 应用表单字段表
         * @table `form_field`
         */
        $this->tableBuilder([
            'name' => 'form_field',
            'comment' => '应用表单字段表',
            'columns' => [
                'id' => $this->primaryKey(),
                'form_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('表单ID'),
                'group_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('分组ID'),
                'name' => $this->char(20)->notNull()->comment('字段标识名称'),
                'label' => $this->string(50)->notNull()->comment('字段名称'),
                'input_type' => $this->char(50)->notNull()->defaultValue('text')->comment('输入框类型'),
                'data_type' => $this->enum(['array', 'date', 'string'])->notNull()->defaultValue('string')->comment('数据类型'),
                'required' => $this->tinyint(1)->unsigned()->notNull()->defaultValue(0)->comment('是否必填'),
                'setting' => $this->text()->comment('字段配置信息'),
                'placeholder' => $this->string(255)->defaultValue('')->comment('说明信息'),
                'listorder' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('显示排序'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(10)->comment('状态'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
                'deleted_at' => $this->unixTimestamp()->comment('删除时间'),
            ],
            'index' => [
                [
                    'columns' => ['form_id', 'name'],
                    'unique' => true
                ],
            ]
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown() {
        $this->dropTable('{{%form_field}}');
        $this->dropTable('{{%form_field_group}}');
        $this->dropTable('{{%form}}');
    }

}
